package com.augrain.microframework.limit;

import com.augrain.microframework.limit.model.LimitType;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.reflect.MethodSignature;

import java.lang.reflect.Method;

/**
 * @author biaoy
 * @since 2020/12/22
 */
public class LimitKeyGenerator {

    /**
     * key前缀生成
     *
     * @param prefix    统一前缀
     * @param limitType 限流类型
     * @param pjp       AOP连接点对象
     * @return 前缀
     */
    public String generate(String prefix, LimitType limitType, ProceedingJoinPoint pjp) {
        MethodSignature signature = (MethodSignature) pjp.getSignature();
        Method method = signature.getMethod();
        String declaringTypeName = signature.getDeclaringTypeName();
        if (limitType == LimitType.METHOD) {
            return prefix + declaringTypeName + "." + method.getName();
        }

        return prefix;
    }
}
